package com.example.jasper.test;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorCompletionService;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRXmlDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;

public class CompileAndGeneratePdf{

	public static void compileJRxml(String jrxmlFileName, String jasperFileName) throws JRException  {
		
		JasperCompileManager.compileReportToFile(jrxmlFileName, jasperFileName);
	}
	
	public static void main(String[] args) throws Exception{
		String xmlDataSource = "C:/TestDs.xml";
		
		String jrxmlFileName = "C:/Test.jrxml";
		String jasperFileName = "C:/Test.jasper"; 
		String outFileName = "C:/Test.pdf";
		HashMap hm = new HashMap();
		
		boolean compile = false;
		
		
		hm.put("ImagePath", "C:/logo_h.JPG");

		if(compile) {
			try {
				compileJRxml(jrxmlFileName, jasperFileName);
				System.out.println("Compiled");
			}catch(JRException e1) {
				System.out.println("Compilation error. i.e:"+e1);
				e1.printStackTrace();
				return;
			}
		}
		
		try {
//			JRXmlDataSource js = new JRXmlDataSource(xmlDataSource,"/Envelope/MXApp_Response/PnLReportList/PnLReport");
			JRXmlDataSource js = new JRXmlDataSource(xmlDataSource,"/Envelope/MXApp_Response/PnLReportList/PnLReport");

			JasperPrint print = JasperFillManager.fillReport(jasperFileName, hm, js); 
			// Create a PDF exporter 
			JRExporter exporter = new JRPdfExporter(); 
			// Configure the exporter (set output file name and print object) 
			exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileName); 
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, print); 
			// Export the PDF file 
			exporter.exportReport(); 
		} catch (JRException e) { 
			e.printStackTrace(); 
		} catch (Exception e) { 
			e.printStackTrace(); 
		}finally {
//			if(in!= null) {
//				try {
//					in.close();
//				}finally{}
//			}
		}
		
		System.out.println("Thanks for using");
	}

}
